package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsCommonService;
import org.darkforest.common.services.GoodsRecommendService;
import org.darkforest.dao.mapper.GoodsRecommendMapper;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.GoodsRecommend;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2016/1/25.
 */
@Service
public class GoodsRecommendServiceImpl implements GoodsRecommendService {


    @Autowired
    private GoodsRecommendMapper goodsRecommendMapper;

    @Autowired
    private GoodsCommonService goodsCommonService;

    @Override
    public int deleteByPrimaryKey(Integer recommendId) {
        return goodsRecommendMapper.deleteByPrimaryKey(recommendId);
    }

    @Override
    public int insert(GoodsRecommend record) {
        return goodsRecommendMapper.insert(record);
    }

    @Override
    public int insertSelective(GoodsRecommend record) {
        return goodsRecommendMapper.insertSelective(record);
    }

    @Override
    public GoodsRecommend selectByPrimaryKey(Integer recommendId) {
        return goodsRecommendMapper.selectByPrimaryKey(recommendId);
    }

    @Override
    public int updateByPrimaryKeySelective(GoodsRecommend record) {
        return goodsRecommendMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(GoodsRecommend record) {
        return goodsRecommendMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(GoodsRecommend record) {
        return goodsRecommendMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<GoodsRecommend> findListByCondition(GoodsRecommend record) {
        return goodsRecommendMapper.findListByCondition(record);
    }

    /**
     * 设置为分类推荐商品
     *
     * @param goodsCommonId
     * @return
     */
    @Override
    public AjaxReturnData doSetRecommendGoods(Integer goodsCommonId,String url) {

        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        GoodsCommon goodsCommon = goodsCommonService.selectById(goodsCommonId);
        List<GoodsRecommend> goodsRecommendList = goodsRecommendMapper.findListByCondition(new GoodsRecommend(goodsCommonId));

        if(goodsCommon.getGoodsState()==1 && goodsRecommendList.size()<1 && goodsCommon.getGoodsVerify()==1){
            GoodsRecommend goodsRecommend = new GoodsRecommend();
            goodsRecommend.setGoodsCommonId(goodsCommon.getGoodsCommonid());
            goodsRecommend.setGoodsName(goodsCommon.getGoodsName());
            goodsRecommend.setGoodsJingle(goodsCommon.getGoodsJingle());
            goodsRecommend.setGcId(goodsCommon.getGcId());
            goodsRecommend.setGcId1(goodsCommon.getGcId1());
            goodsRecommend.setGcId2(goodsCommon.getGcId2());
            goodsRecommend.setGcId3(goodsCommon.getGcId3());
            goodsRecommend.setStoreId(goodsCommon.getStoreId());
            goodsRecommend.setStoreName(goodsCommon.getStoreName());
            goodsRecommend.setBrandId(goodsCommon.getBrandId());
            goodsRecommend.setGoodsImage(goodsCommon.getGoodsImage());
            goodsRecommend.setGoodsPrice(goodsCommon.getGoodsPrice());
            goodsRecommend.setGoodsMarketprice(goodsCommon.getGoodsMarketprice());
            goodsRecommend.setGoodsSerial(goodsCommon.getGoodsSerial());
            goodsRecommend.setGoodsStcids(goodsCommon.getGoodsStcids());
            goodsRecommend.setGoodsType(goodsCommon.getGoodsType());
            goodsRecommend.setRecommendState(1);
            goodsRecommendMapper.insertSelective(goodsRecommend);
            ajaxReturnData.setMessage("设置分类推荐商品成功");
            ajaxReturnData.setResult(true);
            ajaxReturnData.setUrl(url);
        }else{
            ajaxReturnData.setMessage("设置分类推荐商品失败，此商品已经是分类推荐商品或者此商品不满足推荐的规则");
            ajaxReturnData.setResult(false);
        }

        return  ajaxReturnData;
    }
}
